package com.idealighter.game.games.blackjack.struct;

import com.google.common.collect.Lists;

import com.idealighter.game.games.common.AbstractSeat;
import com.idealighter.game.games.common.SeatState;
import com.idealighter.utils.check.EmptyUtil;

import java.util.List;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 21座位 .
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class BlackjackSeat extends AbstractSeat {

  // 所属桌子
  private final BlackjackTable table;
  // 当前结算筹码
  private long billChips = 0;
  // 参与押注
  private boolean hasBet = false;
  // 押注结束
  private boolean betOver = false;
  // 是否参与
  private boolean hasInsurance = false;
  // 是否保险结束
  private boolean insuranceOver = false;
  // 保险下注
  private long insuranceBet = 0;

  // 连续几次没有参与游戏
  private int nobetTimes = 0;
  // 真实输赢
  private long changeBet = 0;

  // 一个座位可能分牌成多个虚拟座位
  private List<BlackjackCardSeat> cardSeats = Lists.newArrayList();


  /**
   * 百家乐位置.
   * 
   * @param order .
   * @param table .
   */
  public BlackjackSeat(int order, BlackjackTable table) {
    super(order);
    this.table = table;
  }


  /**
   * 重置座位数据 .
   */
  public void reset() {
    this.betOver = false;
    this.nobetTimes = 0;
    this.hasBet = false;
    this.billChips = 0;
    this.totalChips = 0;
    this.state = SeatState.SEATED;
    this.cardSeats.clear();
    this.hasInsurance = false;
    this.insuranceBet = 0;
    this.changeBet = 0;
  }

  /**
   * 重置每次的游戏数据数据 .
   */
  public void resetGameData() {
    this.betOver = false;
    this.hasBet = false;
    this.billChips = 0;
    this.hasInsurance = false;
    this.insuranceBet = 0;
    this.state = SeatState.SEATED;
    this.cardSeats.clear();
    this.changeBet = 0;
  }


  /**
   * 重置座位数据除了totalChips .
   */
  public void resetButTotalChips() {
    long totalChips = this.totalChips;
    reset();
    this.totalChips = totalChips;
  }

  /**
   * 清空座位数据 .
   */
  public void clear() {
    this.playerId = 0;
    this.playerName = null;
    reset();
  }

  /**
   * 是否有人坐了.
   *
   * @author abin
   * @date 2018年3月23日 下午12:03:36
   * @return 结果.
   */
  public boolean seated() {
    return playerId > 0;
  }

  /**
   * 当前虚拟手牌数量 .
   *
   * @author abin
   * @date 2018年3月23日 下午4:46:48
   * @return 数量.
   */
  public int getCardSeatsNum() {
    return cardSeats.size();
  }

  /**
   * 获取首次下注筹码 .
   *
   * @author abin
   * @date 2018年3月23日 下午3:35:06
   * @return
   */
  public long getDefaultBet() {
    long firstBet = 0;
    if (EmptyUtil.listIsNotEmpty(cardSeats)) {
      firstBet = cardSeats.get(0).getBet();
    }
    return firstBet;
  }

  /**
   * 默认虚拟手牌位置 .
   *
   * @author abin
   * @date 2018年3月23日 下午4:41:10
   * @return 默认手牌位置.
   */
  public BlackjackCardSeat getDefaultCardSeat() {
    BlackjackCardSeat cardSeat = null;
    if (EmptyUtil.listIsNotEmpty(cardSeats)) {
      cardSeat = cardSeats.get(0);
    }
    return cardSeat;
  }

  /**
   * 是否分牌 .
   *
   * @author abin
   * @date 2018年3月23日 下午7:09:40
   * @return 查看是否分牌.
   */
  public boolean isSplit() {
    return EmptyUtil.listIsNotEmpty(cardSeats) && cardSeats.size() > 1;
  }

  /**
   * 是否所有虚拟座位都已经结算了 .
   *
   * @author abin
   * @date 2018年4月11日 上午10:41:42
   * @return 是否结算.
   */
  public boolean isAllCardSeatBilled() {
    boolean result = true;
    if (EmptyUtil.listIsNotEmpty(cardSeats)) {
      for (BlackjackCardSeat blackjackCardSeat : cardSeats) {
        if (!blackjackCardSeat.isBill()) {
          result = false;
          break;
        }
      }
    }
    return result;
  }

  @SuppressWarnings("unchecked")
  @Override
  public BlackjackTable table() {
    return table;
  }
}
